package view;

import dao.JobInfoDao;
import entity.JobInfo;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

public class ManagerJobFrame extends JFrame {
    private int pageNum = 1;
    private int pageSize = 30;
    private JobInfoDao jobInfoDao = new JobInfoDao();
    private JTable jTable;
    Vector<String> head = new Vector<>();
    JTextField jTextFieldJob;
    JTextField jTextFieldCity;
    public ManagerJobFrame() throws HeadlessException, SQLException, ClassNotFoundException {
        this.setTitle("管理数据");
        //this.setSize(400,300);
        this.setBounds(200,100,1400,800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //头部
        JPanel jPanelHead = new JPanel();
        Dimension dimensionLabel = new Dimension(80,30);
        Dimension dimensionField = new Dimension(240,30);
        JLabel jLabelCity = new JLabel("城市");
         jTextFieldCity = new JTextField();
        JLabel jLabelJob = new JLabel("职位");
        jTextFieldJob = new JTextField();
        JButton jButton = new JButton("搜索");


        jLabelCity.setPreferredSize(dimensionLabel);
        jLabelJob.setPreferredSize(dimensionLabel);
        jTextFieldCity.setPreferredSize(dimensionField);
        jTextFieldJob.setPreferredSize(dimensionField);
        jButton.setPreferredSize(dimensionLabel);
        jPanelHead.add(jLabelCity);
        jPanelHead.add(jTextFieldCity);
        jPanelHead.add(jLabelJob);
        jPanelHead.add(jTextFieldJob);
        jPanelHead.add(jButton);
        this.add(jPanelHead,BorderLayout.NORTH);
        //中间
        jTable = new JTable();
        JScrollPane scrollPane = new JScrollPane(jTable);


        //表格分标题/内容两部分


        jTable.getTableHeader().setFont(new Font("黑体", Font.BOLD, 24));
        //设置表格的样式
        jTable.setFont(new Font("楷体",Font.PLAIN,20));
        jTable.setRowHeight(30);
        //标题数据


        head.add("jobId");
        head.add("jobName");
        head.add("jobArea");
        head.add("jobCompany");
       /* head.add("companyDesc1");
        head.add("companyDesc2");
        head.add("companyDesc3");
        head.add("companyDesc4");*/
        head.add("jobSalary");
     /*   head.add("jobXuli");*/

        Vector<Vector<String>>  jobData = new Vector<>();
        Vector<String> thisJob = new Vector<>();
        thisJob.add("111");
        thisJob.add("111");
        thisJob.add("111");
        thisJob.add("111");
        thisJob.add("111");
        thisJob.add("111");
        thisJob.add("111");
        thisJob.add("111");
        thisJob.add("111");
        thisJob.add("111");
        jobData.add(thisJob);
        jobData.add(thisJob);
        jobData.add(thisJob);
        jobData.add(thisJob);
        DefaultTableModel model = new DefaultTableModel(jobData,head);
        jTable.setModel(model);
        search();
        //scrollPane.add(jTable);
        this.add(scrollPane,BorderLayout.CENTER);


        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               search();
            }
        });

        int count = jobInfoDao.selectCount();
        int pageCount = 0;
        if(count % pageSize == 0){
            pageCount = count/pageSize;
        }else{
            pageCount = count/pageSize+1;
        }
        //底部
        JPanel jPanelBottom = new JPanel();
        JButton prBtn = new JButton("上一页");
        int finalPageCount = pageCount;

        JTextField jTextField = new JTextField(pageNum+"/"+pageCount);
        prBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pageNum ==1){
                    JOptionPane.showMessageDialog(null,"已经是第一页");
                }
                else {
                    pageNum--;
                    jTextField.setText(pageNum + "/" + finalPageCount);
                    search();
                }
            }
        });
        JButton nextBtn = new JButton("下一页");
        prBtn.setPreferredSize(dimensionLabel);
        jTextField.setPreferredSize(dimensionLabel);
        nextBtn.setPreferredSize(dimensionLabel);
        nextBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(pageNum ==finalPageCount){
                    JOptionPane.showMessageDialog(null,"已经是最后一页");
                }
                else{
                    pageNum++;
                    jTextField.setText(pageNum+"/"+ finalPageCount);
                    search();
                }
            }

        });
        jPanelBottom.add(prBtn);
        jPanelBottom.add(jTextField);
        jPanelBottom.add(nextBtn);
        this.add(jPanelBottom,BorderLayout.SOUTH);
    }


    private void search(){
        //获取职位数据
        String jobKw = jTextFieldJob.getText();
        //获取城市编码
        String cityNum = jTextFieldCity.getText();
        //进行搜索
        try {
            List<JobInfo> jobInfos = jobInfoDao.selectByPage(pageNum,pageSize,jobKw,cityNum);
            Vector<Vector<String>> allJob = new Vector<>();
            for(JobInfo jobInfo:jobInfos){

                Vector<String> thisJob = new Vector<>();
                thisJob.add(jobInfo.getJobId());
                thisJob.add(jobInfo.getJobName());
                thisJob.add(jobInfo.getJobArea());
                thisJob.add(jobInfo.getJobCompany());
                thisJob.add(jobInfo.getJobSalary());
                thisJob.add(jobInfo.getJobId());

                allJob.add(thisJob);
            }
            DefaultTableModel model = new DefaultTableModel(allJob,head);
            jTable.setModel(model);


        } catch (SQLException e1) {
            e1.printStackTrace();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        }


    }
}
